import { NextFunction, Response,Request } from "express";

export const defaultErrorHandler=(error:any,request:Request,response:Response,next:NextFunction)=>{
    if(error.message){
        console.log(error.message)
    }

    let statusCode:number,message:string

    switch(error.message){
        case 'NAME_IS_REQUIRED':
            statusCode=400;   // 400 bad request 一个错误的请求
            message='请提供用户名';
            break;
        case 'PASSWORD_IS_REQUIRED':
            statusCode=400;
            message='请提供密码';
            break;
        case 'USER_ALREADY_EXIST':
            statusCode=409;  // 409 conflict 请求与服务器存在冲突
            message='用户名被占用';
            break;
        case 'USER_DOES_NOT_EXIST':
            statusCode=400;
            message='用户不存在';
            break;
        case 'PASSWORD_DOSE_NOT_MATCH':
            statusCode=400;
            message='密码错误'
            break;
        case 'UNAUTHORIZED':
            statusCode=401;
            message='未登录';
            break;
        case 'PASSWORD_IS_THE_SAME':
            statusCode=400;
            message='密码和原来一致'
            break;
        default:
            statusCode=500;
            message='服务器暂时出现问题'
            break;
    }
    response.status(statusCode).send({message})
}